﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Sole
{
    public class Player : Entity
    {
        public float maxSpeed = 1.0f; //usable or not?
        public float rotationSpeed = 0.003f; //usable or not?
        public float acceleration = 0.02f; //usable or not?
        public float speedModifyer = 0.0f; //buffas av courage/shield, nerfas av vissa enemy attacks


        public Color hitColor = Color.Red;
        public int hitTime = 1000; //ms
        public int hitTimeLeft = 0;
        public int shootTime = 500;
        public int shootTimeLeft = 0; //implementera i vapenklass ist //strategy design pattern

        private InputManager inputManager = null;

        private int radian = 20; //radian för collision etc.

        private int courage = 0;
        private int lives = 5;

        private List<Projectile> bullets = null;

        public void SetBulletList(ref List<Projectile> bulletList)
        {
            bullets = bulletList;
        }

        public int Lives
        {
            get { return lives; }
            protected set { lives = value; } //kan inte nås från game, men från den egna hierarkin
        }

        public int Courage
        {
            get { return courage; }
            set { courage = value; }
        }

        public float Speed
        {
            get { return speed; }
            protected set { speed = value; }
        }

        public Player(SpriteBatch spritebatch, Game game)
            : base(spritebatch, game)
        {
            inputManager = new InputManager(game);
        }

        public override void Initialize()
        {
            screenBoundaries = new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);

            position = new Vector2(screenBoundaries.Width * 0.5f, screenBoundaries.Height * 0.5f);

            inputManager.Initialize();

            speed = 5.0f;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            texture = Game.Content.Load<Texture2D>(@"Images\Avatar_placeholder3"); //40pixlar i diameter
            origin = new Vector2(texture.Width, (float)texture.Height * 0.5f); //width / 2 annars?
        }

        public override void Update(GameTime gameTime)
        {
            inputManager.Update(gameTime);

            velocity = Vector2.Zero;

            if (inputManager.IsDown(Keys.Up))
            { velocity.Y = -1; }
            else if (inputManager.IsDown(Keys.Down))
            { velocity.Y = 1; }

            if (inputManager.IsDown(Keys.Left))
            { velocity.X = -1; }
            else if (inputManager.IsDown(Keys.Right))
            { velocity.X = 1; }

            if (velocity.X != 0)
            {
                velocity.X *= speed; //x-led
            }

            if (velocity.Y != 0)
            {
                velocity.Y *= speed; //y-led
            }

            position += velocity;// * (float)gameTime.ElapsedGameTime.Milliseconds;

            if ((position.X - (texture.Width) + (radian)) < screenBoundaries.X)
            {
                position.X = screenBoundaries.X + (texture.Width * 0.5f) + (radian) + 5;//(radian * 3); //screenBoundaries.X + 
            }
            
            if (position.X - (radian) > screenBoundaries.Width)
            {
                position.X = screenBoundaries.Width;// - radian - 2;
            }


            //vaska y-position när collision med backgrounds funkar

            if (position.Y - texture.Height < screenBoundaries.Y)
            {
                position.Y = screenBoundaries.Y;
            }
            else if (position.Y + texture.Height > screenBoundaries.Height)
            {
                position.Y = screenBoundaries.Height;
            }

            ///////////////////////////////////////////

            if (hitTimeLeft > 0)
            {
                hitTimeLeft -= gameTime.ElapsedGameTime.Milliseconds;
                color.G += 20;
                color.B += 40;
                //color = Color.Tomato;
            }
            else
            {
                color = Color.White; // = no tint
            }

            shootTimeLeft -= gameTime.ElapsedGameTime.Milliseconds;
            
            if (shootTimeLeft <= 0 && inputManager.IsDown(Keys.Z))
            {
                Projectile bullet = new Projectile(spritebatch, Game);
                bullet.Initialize();
                bullet.velocity.X = -1; //riktning
                bullet.position.X = position.X - (texture.Width) - (bullet.Texture.Width);
                bullet.position.Y = position.Y;
                bullet.screenBoundaries = screenBoundaries;
                bullets.Add(bullet);

                shootTimeLeft = shootTime;
            }

            if (inputManager.IsDown(Keys.X)) //&& courage == full
            {
                //smart bomb goes here
            }
        }

        public override void OnCollision()
        {
            if (hitTimeLeft <= 0)
            {
                hitTimeLeft = hitTime;
                lives--;
            }

            if (lives <= 0)
            {
                killMe = true;
            }
        }
    }
}